package edu.taru.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.taru.aspectj.LogAspectj;
import edu.taru.common.Constants;
import edu.taru.common.JsonResult;
import edu.taru.pojo.Department;
import edu.taru.service.DepartmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Controller
public class DeptController {

    //声明sl4j 日志
    private static Logger logger = LoggerFactory.getLogger(LogAspectj.class);

    private DepartmentService dservice;

    @Autowired
    public DeptController(DepartmentService dservice){
        this.dservice = dservice;
    }

    /**
     * 查询部门信息，使用排序，使用分页，使用默认值
     * @return
     */
    @RequestMapping(value = "/dept/all",method = RequestMethod.GET)
     public ModelAndView selectDepts (){
        //当前页数
        Integer pageNum = 1;
        //当前每页显示记录数
        Integer pageSize = 10;

        PageHelper.startPage(pageNum,pageSize);
        List<Department> depts = dservice.listDepts();
        PageInfo<Department> pageInfo = new PageInfo<>(depts,pageSize);

        ModelAndView model = new ModelAndView("departmentPage");
        model.addObject("departments",pageInfo);
        return model;
    }

    @RequestMapping(value = "/dept/remove",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult removeDept(HttpSession session){
        JsonResult jsonResult = new JsonResult("删除操作", Constants.STATUS_SUCCESS);
        logger.info("{},{}进行了删除部门操作", LocalDateTime.now(),session.getAttribute("userName"));
       return jsonResult;
    }
}
